<?php
class cmshopapi_Notification extends cmshopApi_base
{
    var $verb='Notification';
    var $notificationUrl=array();  // 可发 url , 多个 ..
    
    
    /***
	 * 发送类 
	 * 	 $sendXmlArr : 数据 数组 ,会被组织成 Xml 
	 * 	 	如果 本来就是 字符串的 ,就认为本来就是  Xml 	 
	 * 	 $returnXml : 返回 值是 数组 或 原生 xml .默认 是 数组 
	 */
    
    function NotificationRequest($sendXmlArr){
		if ($sendXmlArr instanceof SimpleXMLElement ){
	        $sendXmlArr['requestXML']=$sendXmlArr;
	        $requestBody=Helper_xml::simpleArr2xml($sendXmlArr);
	    }elseif(is_array($sendXmlArr)){
			$requestBody=Helper_xml::simpleArr2xml($sendXmlArr);
		}elseif(is_string($sendXmlArr)){
			$requestBody=$sendXmlArr;
		}else{
			throw new Exception_RequestFormatInvalidate(' Any Error');
		}

		//if() socket 发送
		// 2, header 部分 ,Ebay 需要的 Hearder
		$headers='';
		// 3,发送
		$this->contenttype='text/xml';
		if(count($this->notificationUrl)){
            foreach($this->notificationUrl as $notifyurl){
                Helper_tlog::tofile('shopapi_notification.txt',array('url:'.$notifyurl,$requestBody));
                $result=$this->socket_post($notifyurl,$requestBody,$headers,'utf-8',20);
        		$response=$result['body'];
        		Helper_tlog::tofile('shopapi_notification.txt',array($this->config,$result));
		    }
		}else{
            throw new Exception_RequestFormatInvalidate(' Any Error');
        }
//echo $response; 
		//返回 数组
//		$this->_last_response_array=Helper_xml::xmlparse($response);
// 		if($isreturnXml){
			return $response;
// 		}else{
// 			return Helper_xml::xmlparse($response);
// 		}
        
        return false;
    }
    
    /**
     * OrderCreated
     * $order , Orders or order_id  -- 发回时 要取用 order 的信息   
     * app 的 notificationurl 
     */
    function OrderCreated($order,$store_id){
        $event_code='OrderCreated';
        $attr=array(
            'NotificationEventName'=>$event_code,
            'store_id'=>$store_id
        );
        if(!$this->checkNotificationEvent($store_id,$event_code)){
            return false;
        }
        $this->checkNotificationUrl($store_id);
        $response=new cmshopapi_GetOrderTransaction();
        //return
        $response->token['store_id']=$store_id;
        return $this->NotificationRequest($response->Response($order,$attr));
    }
    /**
     * order payed 
     * $order , Orders or order_id  -- 发回时 要取用 order 的信息   
     * app 的 notificationurl 
     * 调用接口在  /model/order :: payed() 中     
     */
    function OrderPayed($order){
        $event_code='OrderPayed';
        $attr=array(
            'NotificationEventName'=>$event_code,
            'store_id'=>$store_id
        );
        if(!$this->checkNotificationEvent($order->store_id,$event_code)){ 
            return false;
        }
        $this->checkNotificationUrl($order->store_id);
        $response=new cmshopapi_GetOrderTransaction();
        return $this->NotificationRequest($response->Response($order,$attr));
    }
    /***
     * 是否使用 Notification
     */         
    function checkNotificationEvent($store_id,$event_code){
        $sn=Store_Notificationevents::find('[store_id]=? And [event.event_code]=? And status&1',$store_id,$event_code)->getOne();
        if($sn->isNewRecord()) return false;
        return true;
    }
    /** 
     *  checkNotificationUrl 
     *  取得 NotificationUrl
     */         
    function checkNotificationUrl($store_id){
        if($store_id instanceof Store_Notificationapps){
            $sn=$store_id->toArray();
            foreach($sn as $n){
                $this->notificationUrl[$n->app->app_id]=$n->app->notificationurl;
            }
        }elseif(is_int($store_id)||strlen($store_id)){
            $sn=Store_Notificationapps::find('store_id=? And status&1',$store_id)->getAll();
            foreach($sn as $n){
                if(strlen($n->app->notificationurl)<=0) continue;
                $this->notificationUrl[$n->app->app_id]=$n->app->notificationurl;
            }
        }
        return true;
    }
}
